<#  
.SYNOPSIS  
    This PowerShell Script changes the Quota of a SiteColletion.
.DESCRIPTION  
    PowerShell Script that changes the Quota of a SiteColletion.
	
.NOTES  
    Author         : Ayoub Umoru
    Prerequisite   : Local installation of SharePoint; Loaded SharePoint Modules

.PARAMETER Identity
	Target Site Collection

.PARAMETER Percentage
	Percentage of Increase or Decrease (150 = 50% increase; 50 = 50% decrease)
	
.EXAMPLE  
	PS D:\> .\AdjustSiteQuota.ps1 -Identity http://target.site.collection -Percentage 150

.LINK 
	https://sharepointadjustsitequota.codeplex.com
#>
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$Identity,
	
   [Parameter(Mandatory=$True)]
   [string]$Percentage,
   
   [switch]$DoNothing
)

# Input validation
If ( ($Identity -eq "") -or ($Percentage -lt 0))
{
	Write-Host "Please supply valid parameters (positive percentage, valid site url...)" -foregroundcolor red
	exit
}

$site = Get-SPSite $Identity


if ( $site -eq $null )
{
	Write-Host "Please supply valid parameters (valid site url...)" -foregroundcolor red
	exit
}
$db = $site | Get-SPContentDataBase

$warningSize = $site.Quota.StorageWarningLevel
$newWarningSize = ($warningSize / 100) * $Percentage

$maxSize = $site.Quota.StorageMaximumLevel
$newMaxSize = ($maxSize / 100) * $Percentage

Write-Host "`nStatus`n" -foregroundcolor yellow
Write-Host "$([System.Math]::Round((($site.Quota.StorageWarningLevel-$site.Usage.Storage)/1mb),1)) MB to reach the Warning level" -foregroundcolor yellow

Write-Host "$([System.Math]::Round((($site.Quota.StorageMaximumLevel-$site.Usage.Storage)/1mb),1)) MB to reach the Maximum level" -foregroundcolor yellow


Write-Host "`n`nSettings`n" -foregroundcolor yellow



Write-Host "Current DB size   $([System.Math]::Round($db.DiskSizeRequired/1mb,1)) MB" -foregroundcolor yellow
Write-Host "Current Site size $([System.Math]::Round($site.Usage.Storage/1mb,1)) MB" -foregroundcolor yellow
Write-Host "Quota Warning     $([System.Math]::Round($site.Quota.StorageWarningLevel/1mb,1)) MB" -foregroundcolor yellow
Write-Host "Quota Maximum     $([System.Math]::Round($site.Quota.StorageMaximumLevel/1mb,1)) MB"-foregroundcolor yellow

Write-Host "`nDatabase server $($db.NormalizedDataSource)" -foregroundcolor yellow
Write-Host "Database name     $($db.DisplayName)" -foregroundcolor yellow
Write-Host "`nRBS Enabled     $($db.RemoteBlobStorageSettings.Enabled)" -foregroundcolor yellow


$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
$caption = "Are you sure you want to increase quota for $($site.url) ?"
$message = "Do you want to change quota from Warning=$([System.Math]::Round($warningSize/1mb,1)) MB MaxSize=$([System.Math]::Round($maxSize/1mb,1)) MB to Warning=$([System.Math]::Round($newWarningSize/1mb,1)) MB MaxSize=$([System.Math]::Round($newMaxSize/1mb,1)) MB ?"


$result = $Host.UI.PromptForChoice($caption,$message,$choices,0)

if($result -eq 0) 
{ 
	Write-Host "`nIncreasing quota for site $($site.url)`n" -foregroundcolor yellow
	Write-Host "New StorageWarningLevel = $([System.Math]::Round($newWarningSize/1mb,1))" -foregroundcolor green
	Write-Host "New StorageMaximumLevel = $([System.Math]::Round($newMaxSize/1mb,1))" -foregroundcolor green
	
	if ( $DoNothing -eq $true)
	{
		Write-Host "Simulation mode is enabled!" -foregroundcolor red
	}
	else{
		Set-SPSite -Identity $($site.url) -MaxSize $newMaxSize -WarningSize $newWarningSize
	}
	
	
	$site.Dispose()
	$site = Get-SPSite $Identity
	Write-Host "`n`nRefreshed settings " -foregroundcolor White
	
	Write-Host "Quota Warning   $([System.Math]::Round($site.Quota.StorageWarningLevel/1mb,1))" -foregroundcolor yellow
	Write-Host "Quota Maximum   $([System.Math]::Round($site.Quota.StorageMaximumLevel/1mb,1))"-foregroundcolor yellow
}

if($result -eq 1) 
{ 
	Write-Host "bye" 
	exit
}


